package com.example.student.http;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.widget.Toast;

//import com.funrace.shop.original.common.constant.HttpConstant;
//import com.funrace.shop.original.common.utils.LogUtils;

/**
 * @描述: 系统Handler
 * @创建人: guoyanlu
 * @创建时间： 2016/12/07 14:39
 */
public class FSSHandler extends Handler {

    private String TAG="FSSHandler";
    private static Context context;
    private FSSCallbackListener<Object> callbackListener;

    public FSSHandler(Context context, FSSCallbackListener<Object> callbackListener) {
        FSSHandler.context = context;
        this.callbackListener = callbackListener;
    }

    @Override
    public void handleMessage(Message msg) {
        FSSResponse akecResp = (FSSResponse) msg.obj;
        switch (akecResp.getRespCode()) {
            case HttpConstant.RESPONSE_SUCCESS:
                //响应成功，返回处理
//                LogUtils.e(HttpConstant.TAG,"///响应成功。。。。啦"+akecResp.getRespObj().toString());
                callbackListener.onSuccess(akecResp.getRespObj());
                break;
            case HttpConstant.RESPONSE_FAILURE:
                //响应失败,统一提示处理
//                showToast(akecResp.getRespMsg());
                Log.e(TAG,"应失败,统一提示处理akecResp:"+akecResp.getRespMsg());
                String respMsg = akecResp.getRespMsg();
                Log.e(TAG,"//...失败的回调"+respMsg);
                callbackListener.onFailure(respMsg);
                /*
                try {
                    Object respObj = akecResp.getRespObj();
                    Map<String, String> respMap = (Map<String, String>) respObj;
                    String code = respMap.get(BaseResponse.CODE);
                    if (RespCodeMsgConstant.CODE_USER_LOGIN_FAIL.equals(code)) {
                        //显示提示框
                        if(alertDialog != null){
                            alertDialog.dismiss();
                        }
                        alertDialog = showAlertDialog(context, "系统提示",akecResp.getRespMsg() , new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                alertDialog.dismiss();
                                //用户登录失效跳转到登录页面
                                Intent loginIntent = new Intent(context, LoginActivity.class);
                                loginIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);//Intent.FLAG_ACTIVITY_CLEAR_TOP |
                                context.startActivity(loginIntent);

                            }
                        }, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                alertDialog.dismiss();
                            }
                        });

                    }
                } catch (Exception e) {
                    //只提示，不跳转
                    return;
                }
                */
                break;
        }

        super.handleMessage(msg);

    }
/**
 * @描述 弹出Toast提示框
 * @param msg 提示信息
 * @return
 * @创建人 guoyanlu
 * @创建时间 2016/12/29 11:24
 */
    public void showToast(String msg) {
        Toast toast = Toast.makeText(context, msg, Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
    }

    /**
     * @描述 自定义系统提示框
     * @创建人 jh
     * @创建时间 2016/7/19
     */
    /*
    public AlertDialog showAlertDialog(Context context, String title, String content, View.OnClickListener okClickListener, View.OnClickListener cancleClickListener) {

        final AlertDialog.Builder builder = new AlertDialog.Builder(context);
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_alert, null);
        builder.setView(view);
        Button okBtn = (Button) view.findViewById(R.id.ok_btn);
        Button cancelBtn = (Button) view.findViewById(R.id.cancel_btn);
        TextView contentTextView = (TextView) view.findViewById(R.id.content_textview);
        TextView titleTextView = (TextView) view.findViewById(R.id.title_textview);
        titleTextView.setText(title);
        contentTextView.setText(content);
        builder.setCancelable(false);
        okBtn.setOnClickListener(okClickListener);
        cancelBtn.setOnClickListener(cancleClickListener);
        AlertDialog alertDialog = builder.create();
        if (alertDialog.isShowing()) {
            alertDialog.dismiss();
        }
        alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        alertDialog.show();
        return alertDialog;
    }
    */

}
